package com.zhuoyue.biz.core.api.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhuoyue.biz.core.api.domain.AppUserDO;
import com.zhuoyue.biz.core.api.domain.AppUserHomeRelationDO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author wjming
 * @ClassName: AppUserHomeRelationDao
 * @Description: 用户家庭关系底层数据
 * @Date 2021-07-08 13:33
 */
public interface AppUserHomeRelationDao extends BaseMapper<AppUserHomeRelationDO> {

    /**
     * 删除分享关系
     *
     * @param appUserId
     * @param homeId
     * @param relationType
     * @return
     */
    @Delete("delete from tbl_app_user_home_relation where app_user_id = #{appUserId} " +
            "and home_id = #{homeId} and relation_type =#{relationType}")
    int deleteRelation(@Param("appUserId") String appUserId, @Param("homeId") String homeId, @Param("relationType") int relationType);

    /**
     * 查询用户的家庭数量
     *
     * @param appUserId
     * @return
     */
    int selectCountByAppUserId(String appUserId);

    /**
     * 修改我的若琪
     *
     * @param relation
     * @return
     */
    int updateRokid(AppUserHomeRelationDO relation);


    /**
     * 修改我的声必可
     *
     * @param relation
     * @return
     */
    int updateAiSpeaker(AppUserHomeRelationDO relation);


    /**
     * 修改我的小度
     *
     * @param relation
     * @return
     */
    int updateDuerOs(AppUserHomeRelationDO relation);


    /**
     * 修改我的天猫
     *
     * @param relation
     * @return
     */
    int updateTmall(AppUserHomeRelationDO relation);


    /**
     * 通过homeId查询到appUserId并把当前房间对应的appUserId修改为转移的appUserId
     *
     * @param appUserHomeRelationDO
     * @return
     */
    int updateAppuserIdByHomeId(AppUserHomeRelationDO appUserHomeRelationDO);

    /**
     * 获取子账号信息
     *
     * @param childAccount
     * @param organizationCode
     * @return
     */
    @Select("select * from tbl_app_user where account=#{childAccount} and  organization_code=#{organizationCode}")
    AppUserDO getAccountMessage(@Param("childAccount") String childAccount, @Param("organizationCode") String organizationCode);

    /**
     * 获取家庭名称通过ID
     *
     * @param masterHomeId
     * @return
     */
    @Select("select name from tbl_home where id=#{masterHomeId}")
    String getHomeNameById(@Param("masterHomeId") String masterHomeId);

    /**
     * 获取用户昵称
     *
     * @param masterId
     * @return
     */
    @Select("select nick_name from tbl_app_user where id=#{masterId}")
    String getUserName(@Param("masterId") String masterId);

    /**
     * 通过家庭ID获取主人的信息
     *
     * @param homeId
     * @return
     */
    @Select("select user.* from tbl_app_user_home_relation relation left join tbl_app_user user on user.id=relation.app_user_id " +
            "where relation.home_id =#{homeId} and relation.relation_type='0'")
    AppUserDO getMasterSourceId(@Param("homeId") String homeId);

    /**
     * 获取子账号信息
     *
     * @param homeId
     * @return
     */
    @Select("select user.account,user.nick_name,user.img_url,user.organization_code from tbl_app_user_home_relation relation left join tbl_app_user user on user.id=relation.app_user_id " +
            "where relation.home_id =#{homeId} and relation.relation_type='1'")
    List<AppUserDO> selectChildUserList(@Param("homeId") String homeId);
}
